<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="tree" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="getId('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 使用el-tree 放主体内容-->
        <el-tree :data="list" :default-expand-all="false" @node-click="handleNodeClick">
          <!--  作用域插槽-->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%">
              <el-col :span="20">
                <svg-icon icon-class="user" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="getId(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="getIdEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && !data.children.length" @click.native="hDelete(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 实现新增子部门或者编辑部门的对话框     -->
      <el-dialog
        :title="isEdit?'编辑部门':'添加子部门'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        @close="resetForm"
      >
        <!-- 解决数据不更新的问题:方法一 v-if="showDialog" -->
        <depDialog :id="curId" ref="addOrEditRef" :origin-list="originList" :is-edit="isEdit" @depClose="showDialog=false" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, deleteDepartment } from '@/api/department'

import { arr2tree } from '@/utils'
// 导入子组件
import depDialog from './depDialog.vue'

export default {
  name: '',
  // 导入组件名
  components: {
    depDialog
  },
  // 父传子,接收变量名
  props: {},
  data() {
    return {
      // 树形结构数组
      list: [],
      // 控制弹框显示隐藏
      showDialog: false,
      curId: '', // 新增部门需要的pid
      isEdit: false, // false新增部门 true编辑部门
      originList: [] // 部门的原始数据 {id,name,pid,code}
    }
  },
  // 计算属性 类似于 data 概念
  computed: {},
  // 监听器 监控 data 中的数据变化
  watch: {},
  created() {
    this.loadDepartments()
  },
  // 方法集合
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    hSuccess() {
      // 重新渲染
      this.loadDepartments()
      // 关闭对话框
      this.showDialog = false
    },
    // 新增部门 传递给表单pid
    getId(id) {
      this.isEdit = false
      this.curId = id
      this.showDialog = true
    },
    // 编辑部门
    getIdEdit(id) {
      this.isEdit = true
      this.curId = id
      this.showDialog = true
      // dom更新是异步执行 这里的代码是在每次编辑部门的时候获取一次数据
      // this.$nextTick(() => {}) 是等待dom更新后才执行的回调函数
      this.$nextTick(() => {
        this.$refs.addOrEditRef.loadDepartmentDetail()
      })
    },
    // 删除部门
    hDelete(id) {
      // 弹框确定
      this.$confirm('您确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteDepartment(id)
        // 重新渲染
        this.loadDepartments()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 获取部门列表
    async loadDepartments() {
      try {
        const { data: res } = await getDepartments()
        // 对原始数据进行一个筛的选
        res.depts.shift()
        this.originList = res.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // console.log(res)
        this.list = arr2tree(res.depts)
      } catch (error) {
        return new Error(error)
      }
    },
    // 表单关闭事件 重置表单
    resetForm() {
      // 调用子组件中的重置表单方法
      this.$refs.addOrEditRef.resetForm()
    }
  }
}
</script>

<style scoped>
</style>
